package com.ywqf_api.service.impl;

import com.ywqf_api.base.BaseException;
import com.ywqf_api.dao.GoodsDao;
import com.ywqf_api.dto.exc.ClientExcution;
import com.ywqf_api.dto.in.GoodsDecorationDto;
import com.ywqf_api.dto.in.GoodsInfoDto;
import com.ywqf_api.entity.*;
import com.ywqf_api.enums.ClientEnum;
import com.ywqf_api.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Override
    public Goods findGoodsInfo(Integer goodId) {
        Goods findGoodsInfo = goodsDao.findGoodsInfo(goodId);
        return findGoodsInfo;
    }

    @Override
    public Map findCommunity(int goodsId) {
        List<GoodsCommunity> findCommunity=goodsDao.findCommunity(goodsId);
        List<CoodsPic> findGoodsShowPic=goodsDao.findGoodsShowPic(goodsId);
        List<CoodsPic>  goodsDetailsPic=goodsDao.goodsDetailsPic(goodsId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("findCommunity",findCommunity);
        map.put("findGoodsShowPic",findGoodsShowPic);
        map.put("goodsDetailsPic",goodsDetailsPic);
        return map;
    }



    @Override
    @Transactional
    public int addCommunity(GoodsInfoDto goodsInfoDto) {
        Random random = new Random();
        int fourRandom = random.nextInt(90000) + 10000;
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        orderNum = "HC"+longstr2+fourRandom;
        goodsInfoDto.setOrderNum(orderNum);
        int addCommunity=goodsDao.addCommunity(goodsInfoDto);
        if (addCommunity>0){
            int addGoods=goodsDao.addGoods(goodsInfoDto);
            if (addGoods>0){
                return 1;
            }else {
                throw new BaseException("添加失败");
            }
        }else {
            throw new BaseException("添加失败");
        }

    }

    @Override
    public Map findDecoration(int goodsId) {
        List<GoodsDecoration> findDecoration=goodsDao.findDecoration(goodsId);
        List<CoodsPic> findGoodsShowPic=goodsDao.findGoodsShowPic(goodsId);
        List<CoodsPic>  goodsDetailsPic=goodsDao.goodsDetailsPic(goodsId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("findCommunity",findDecoration);
        map.put("findGoodsShowPic",findGoodsShowPic);
        map.put("goodsDetailsPic",goodsDetailsPic);
        return map;
    }

    @Override
    public int addDecoration(GoodsDecorationDto goodsDecorationDto){
        Random random = new Random();
        int fourRandom = random.nextInt(90000) + 10000;
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        orderNum = "HC"+longstr2+fourRandom;
        goodsDecorationDto.setOrderNum(orderNum);
        int addDecoration=goodsDao.addDecoration(goodsDecorationDto);
        if (addDecoration>0){
            int addDecorationGoods=goodsDao.addDecorationGoods(goodsDecorationDto);
            if (addDecorationGoods>0){
                return 1;
            }else {
                throw new BaseException("添加失败");
            }
        }else {
            throw new BaseException("添加失败");
        }

    }

    @Override
    public int findIsReservation(Integer customerId, Integer goodsId) {
        Integer find = goodsDao.findIsReservation(customerId,goodsId);
        if(find == null || find == 0){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public List findGoodsList(int customerId ,int type) {
        List<GoodsList> findGoodsList=goodsDao.findGoodsList(customerId,type);
        if (findGoodsList==null||findGoodsList.size()==0){
            return null;
        }
        for (int i=0;i<findGoodsList.size();i++){
            List array=new ArrayList();
            GoodsList aaa=findGoodsList.get(i);
            if(aaa.getOrderTime().length() >= 19){
                aaa.setOrderTime(aaa.getOrderTime().substring(0,19));
            }
            array.add(aaa);
        }

        return findGoodsList;
    }
}
